package com.medical.data.controller;


import com.medical.data.service.UploadService;
import com.medical.data.service.UserService;
import com.medical.data.utils.Result;
import com.medical.data.vo.UpdatePasswordVo;
import com.medical.data.vo.UserInformationVo;
import com.medical.data.vo.UserLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/front/user")
@Api(description = "用户管理")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UploadService uploadService;

    @ApiOperation("登录接口")
    @ApiImplicitParam(name = "userLoginVo", required = true, paramType = "body", dataTypeClass = UserLoginVo.class)
    @PostMapping("/loginUser")
    public Result login(@Validated @RequestBody UserLoginVo userLoginVo) {
        log.info("/front/user/loginUser参数========>{}", userLoginVo);
        String token = userService.login(userLoginVo);
        //这里有一个异步通知
        //Message message = MessageBuilder.withBody(token.getBytes(StandardCharsets.UTF_8)).setMessageId(UUID.fastUUID().toString()).build();
        // rabbitTemplate.convertAndSend(RabbitMQConfig.LOGIN_CUL_DISTANCT,message);
        return Result.ok().data("token", token);
    }

    @ApiOperation("个人信息查看接口")
    @PostMapping("/information")
    public Result information() {
        log.info("/front/user/information参数========>{}", "无");
        UserInformationVo userInformationVo = userService.getUserById();
        return Result.ok().data("User", userInformationVo);
    }

    @ApiOperation("个人信息修改接口")
    @ApiImplicitParam(name = "UserInformationVo", required = true, paramType = "body", dataTypeClass = UserInformationVo.class)
    @PostMapping("/change")
    public Result change(@Validated @RequestBody UserInformationVo userInformationVo) {
        log.info("/front/user/change参数========>{}", userInformationVo);
        userService.updateUser(userInformationVo);
        return Result.ok().data("message", "修改成功");
    }

    @ApiOperation("头像信息修改接口")
    @PostMapping("/changeAvatar")
    public Result changeAvatar(@RequestParam MultipartFile file) {
        log.info("/front/user/changeAvatar参数========>{}", file);
        userService.updateAvatar(file, "test");
        return Result.ok().data("message", "修改成功");
    }

    @ApiOperation("用户注册")
    @ApiImplicitParam(name = "UserLoginVo", required = true, paramType = "body", dataTypeClass = UserLoginVo.class)
    @PostMapping("/reg")
    public Result reg(@Validated @RequestBody UserLoginVo userLoginVo) {
        log.info("/front/user/reg参数========>{}", userLoginVo);
        userService.reg(userLoginVo);
        return Result.ok();
    }

    @ApiOperation("上传头像")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String url = uploadService.uploadImage(file, "avatar", request, "avatar");
        return Result.ok().data("url", url);
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@Validated @RequestBody UpdatePasswordVo updatePasswordVo) {
        log.info("/front/user/updatePassword参数========>{}", updatePasswordVo);
        userService.updatePassword(updatePasswordVo);
        return Result.ok();
    }

}
